package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZLJ
 * @date 2025/10/7 18:13
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/users")
@SuppressWarnings("all")
public class UserController {

    @Autowired
    private UserService userService;

    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable("ids") List<Integer> ids){
        userService.deleteBatch(ids);
        log.info("批量删除用户id:{}",ids);
        return Result.success();
    }

    @GetMapping("/dept/{deptId}")
    public Result getByDept(@PathVariable("deptId") Integer deptId){
        List<UserDO> list = userService.getByDept(deptId);
        log.info("查询用户部门:{}",deptId);
        return Result.success(list);
    }

    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable("roleLabel") String roleLabel){
        List<UserDO> list = userService.listByRole(roleLabel);
        log.info("查询用户角色:{}",roleLabel);
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result selectAll(){
        List<UserDO> list = userService.selectAll();
        log.info("查询用户列表");
        return Result.success(list);
    }

    /*@DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        userService.deleteById(id);
        log.info("删除用户id:{}",id);
        return Result.success();
    }*/


    @PutMapping
    public Result update(@RequestBody User user) {
        userService.update(user);
        log.info("修改用户:{}",user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        UserDO user = userService.getById(id);
        log.info("查询用户id:{}",id);
        return Result.success(user);
    }

    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        log.info("新增用户:{}",user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto){
        PageResult<UserDO> pageResult = userService.list(dto);
        log.info("分页查询用户,参数:用户名称={},用户状态={},当前页码={},每页大小={}", dto.getName(), dto.getStatus(), dto);
        return Result.success(pageResult);
    }
}
